package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.SkuInfoDao;
import com.atguigu.gulimall.product.entity.SkuImagesEntity;
import com.atguigu.gulimall.product.entity.SkuInfoEntity;
import com.atguigu.gulimall.product.entity.SpuInfoDescEntity;
import com.atguigu.gulimall.product.service.SkuImagesService;
import com.atguigu.gulimall.product.service.SkuInfoService;
import com.atguigu.gulimall.product.service.SkuSaleAttrValueService;
import com.atguigu.gulimall.product.service.SpuInfoDescService;
import com.atguigu.gulimall.product.vo.Attr;
import com.atguigu.gulimall.product.vo.SkuAttrVo;
import com.atguigu.gulimall.product.vo.SkuItemVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    private SkuImagesService skuImagesService;
    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;
    @Autowired
    private SpuInfoDescService spuInfoDescService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * sku检索分页
     * @param params
     * @return
     * key: '华为',//检索关键字
    catelogId: 0,
    brandId: 0,
    min: 0,
    max: 0
     */

    @Override
    public PageUtils queryPageList(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.and(wap->{
                wap.eq("id",key).or().like("sku_name",key);
            });
        }
        String catelogId = (String) params.get("catelogId");
        if (!StringUtils.isEmpty(catelogId)&&!"0".equalsIgnoreCase(catelogId)){
            wrapper.eq("catalog_id",catelogId);
        }
        String brandId = (String) params.get("brandId");
        if (!StringUtils.isEmpty(catelogId)&&!"0".equalsIgnoreCase(brandId)){
            wrapper.eq("brand_id",brandId);
        }
        String min = (String) params.get("min");
        if (!StringUtils.isEmpty(catelogId)){
            wrapper.ge("price",min);
        }
        String max = (String) params.get("max");
        if (!StringUtils.isEmpty(catelogId)){
            BigDecimal bigDecimal = new BigDecimal(max);
            if (bigDecimal.compareTo(new BigDecimal("0"))==1){
                wrapper.le("price",max);
            }
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    /**
     * 根据spuid查询sku的商品信息
     * @param spuId
     * @return
     */
    @Override
    public List<SkuInfoEntity> selectList(Long spuId) {
        List<SkuInfoEntity> spu_id = this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));

        return spu_id;
    }

    /**
     * 以spuid查询sku信息
     * @param skuId
     */
    @Override
    public SkuItemVo selectSpuID(Long skuId) {
        SkuItemVo skuItemVo = new SkuItemVo();
        //获取sku的基本信息
        SkuInfoEntity skuInfoEntity = this.baseMapper.selectById(skuId);
        skuItemVo.setSkuInfoEntities(skuInfoEntity);
        //获取sku的图片信息
        List<SkuImagesEntity> skuImagesEntity = skuImagesService.selectImages(skuId);
        skuItemVo.setSkuImagesEntities(skuImagesEntity);
        //获取sku的规格参数
        List<SkuAttrVo> vo = this.baseMapper.selectAttr(skuInfoEntity.getCatalogId(),skuInfoEntity.getSpuId());
        skuItemVo.setSkuAttrVos(vo);
        //获取sku的销售属性
        List<Attr> attrs = skuSaleAttrValueService.selectAttr(skuInfoEntity.getSpuId());
        skuItemVo.setAttrs(attrs);


        //获取spu的介绍
        SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.selectById(skuInfoEntity.getSpuId());
        skuItemVo.setSpuInfoDescEntity(spuInfoDescEntity);
        return skuItemVo;

    }

    /**
     * 获取商品单价信息
     * @param skuId
     * @return
     */
    @Override
    public BigDecimal getPrice(Long skuId) {
        return this.baseMapper.selectIdPrice(skuId);
    }

}